var $ = require('jquery');
var util = require('imutil');
var Backbone = require('backbone');
var tpl = require('../tpl');

var Messages = require('../model/messages');
var Message = require('../model/message');
var dialog = require('dialog');
var bus = require('bus');
var config = require('../webim.config');

module.exports = Backbone.View.extend({
    el: null,
    model: null,
    events: {

    },
    initialize: function(options, api, uid,myHeadImg,otherHeadImg){
        var that = this;
        this.api = api;
        this.uid = uid;
        this.myHeadImg =myHeadImg ;
        this.otherHeadImg = otherHeadImg;

        this.listenTo(this.model, 'change', this.render);

        this.render();
    },
    render: function(){
        
        var that = this;
        var model = this.model.toJSON();
        var textVal = model.msg;
        
        if(this.api.sdk.isProductLink(textVal) && model.explainProd == true){
            // 显示链接消息
            var renderLinkMsg = function(){
                if(that.api.sdk.isUrl(textVal)){
                    that.model.set({msg:that.api.sdk.completeUrl(textVal)}, {silent: true});
                }
                that.$el.html(tpl.chat2.chatMsg1({
                    message: that.model.toJSON(),
                    mineId:that.api.sdk.userId,
                    otherId:that.uid,
                    msgcid:that.model.cid,
                    myHeadImg:that.myHeadImg,
                    otherHeadImg:that.otherHeadImg
                }));
            };
            
            this.api.sdk.getProductInfoByLink(textVal,function(info){
                if(info && info.name){
                    that.$el.html(tpl.chat2.chatMsg2({
                        nophoto: config.nophoto,
                        message: that.model.toJSON(),
                        mineId:that.api.sdk.userId,
                        otherId:that.uid,
                        product:info,
                        msgcid:that.model.cid,
                        myHeadImg:that.myHeadImg,
                        otherHeadImg:that.otherHeadImg,
                        link: textVal
                    }));
                }else{ // 解析失败
                    renderLinkMsg();
                }
                
            }, function(e){ // 解析失败，显示原链接
                renderLinkMsg();
            });

        }else{
            if(this.api.sdk.isUrl(textVal)){
                this.model.set({msg:this.api.sdk.completeUrl(textVal)}, {silent: true});
            }
            
            var msgMeta = model.msgMeta;
            if(that.api.sdk.isImageMessage(model) && msgMeta && msgMeta.c && msgMeta.c.m){
                msgMeta.c.m.forEach(function(e){
                    model.msg = model.msg.replace("\/:b0",'<img src="'+that.api.sdk.imgLink(e.i)+'"/>') ;
                })
                //that.model.set({msg:textVal});
            }

            this.$el.html(tpl.chat2.chatMsg1({
                message: model,
                mineId:this.api.sdk.userId,
                otherId:this.uid,
                msgcid:this.model.cid,
                myHeadImg:this.myHeadImg,
                otherHeadImg:this.otherHeadImg
            }));
        }

        return this;
    }
});